// JavaScript Document

var AINSPECTOR = {};
var first=true;
var prevNode;
var default_width = 300;
OpenAjax.a11y.cache.updateLinkCache( parent.document );﻿  //updating links in cache
OpenAjax.a11y.cache.sortLinkCache( 'order' ); //sorting links in cache

// generates iframe. not currently being used.
AINSPECTOR.generateIframe = function ()
{
﻿  ﻿  var obj = document.createElement("iframe");
﻿  ﻿  obj.setAttribute("scrolling","yes");
﻿  ﻿  obj.noResize = false;
﻿  ﻿  obj.style.position = "fixed";
﻿  ﻿  obj.style.top = "80%"
﻿  ﻿  obj.id = 'ainspector-frame';
﻿  ﻿  obj.style.width = "100%";
﻿  ﻿  obj.style.height = "20%";
﻿  ﻿  obj.style.borderStyle = "solid";
﻿  ﻿  obj.style.borderWidth = "10px";
﻿  ﻿  obj.style.borderColor = "red";
﻿  ﻿  obj.style.backgroundColor = "white";
﻿  ﻿  //obj.src = path + 'display.html';
﻿  ﻿  //alert(obj.src);
﻿  ﻿  return obj;
}

//finds the y value of some object
function findPos(obj) {
﻿  var curtop = 0;
﻿  if (obj.offsetParent) {
﻿  ﻿  do {
﻿  ﻿  ﻿  curtop += obj.offsetTop;
﻿  ﻿  } while (obj = obj.offsetParent);
﻿  return [curtop];
﻿  }
}

// highlights respective link on page
AINSPECTOR.highlight = function ( i ) {
  if(first==false)
  {
  prevNode.style.borderStyle="none";
  }
  else first=false;
  var tmp = OpenAjax.a11y.cache.links[i].node;
  tmp.style.borderStyle="solid";
  tmp.style.borderWidth="10px";
  prevNode = tmp;
window.scroll(0,findPos(OpenAjax.a11y.cache.links[i].node));  //event.stopPropagation();
  //event.preventDefault();﻿  
  return true;
}


// Makes iframe with a data table and attaches it to the body of the document
AINSPECTOR.makeLinkIframe = function ()
{
﻿  var frame = AINSPECTOR.generateIframe();
﻿  document.body.appendChild(frame);
  var newTable = AINSPECTOR.makeTable();
  frame.contentDocument.body.appendChild(newTable);
}

// creates table with data and returns it to caller
AINSPECTOR.makeTable = function()
{
   var srctable = document.createElement("table");
   srctable.border = 5;
   srctable.borderColor = "black";
   srctable.setAttribute('role','grid'); //set ARIA role for table
   AINSPECTOR.fillUpTableData(srctable);
   AINSPECTOR.insertTableHeader(srctable);
  
   return srctable;
}

// inserts header into table given
AINSPECTOR.insertTableHeader = function(srctable)
{
if (!srctable.tHead) {
var header = srctable.createTHead();
var row = header.insertRow(0);
row.tabIndex = 1;
appendHeaderCell(row,0,"Index");          
appendHeaderCell(row,1,"Accessible Text");
﻿appendHeaderCell(row,2,"URL");
﻿appendHeaderCell(row,3,"Height");
﻿appendHeaderCell(row,4,"Width");
}
}

function appendHeaderCell(row,index,content)
{
  var tmpcell = row.insertCell(index);
﻿  tmpcell.setAttribute('role','columnheader');	
  tmpcell.innerText = content;
  tmpcell.tabIndex = 1;
﻿  return tmpcell;	
}

// fills up iframe table with link details
AINSPECTOR.fillUpTableData = function(srctable)
{	
﻿   for(var i = 0; i < OpenAjax.a11y.cache.links.length; i++ ) {
	﻿var elem = OpenAjax.a11y.cache.links[i];
	﻿tmprow = appendRow(srctable,i);
	tmprow.setAttribute( 'role', 'row');
	tmprow.tabIndex = 1;
﻿  ﻿  var index = "" + (i+1); 
﻿  ﻿  appendCell(tmprow,0,index,"50px");
﻿  ﻿  appendCell(tmprow,1,elem.accessible_text);
﻿  ﻿  appendCell(tmprow,2,elem.href);
﻿  ﻿  appendCell(tmprow,3,elem.height,"100px");
﻿  ﻿  appendCell(tmprow,4,elem.width,"100px");
﻿  ﻿  tmprow.setAttribute("onclick","parent.AINSPECTOR.highlight(" + i + ")");
	tmprow.setAttribute("onfocus","this.style.backgroundColor = '#0000CD'");
	tmprow.setAttribute("onfocusout","this.style.backgroundColor= 'transparent'");
	﻿ tmprow = null;
   } // endfor﻿  ﻿  
   
}

// appends a row to the table
function appendRow(srctable,i)
{
return srctable.insertRow(i);
}

//appends a cell to the table with content argument provided
function appendCell(row,i,content,width)
{
﻿  var tmpcell = row.insertCell(i);
  tmpcell.tabIndex = 1;
﻿  tmpcell.setAttribute('role','gridcell');	
  tmpcell.innerText = content;
  tmpcell.setAttribute("onfocus","this.parentNode.style.backgroundColor = '#0000CD'");
  tmpcell.setAttribute("onfocusout","this.parentNode.style.backgroundColor= 'transparent'");
﻿  if(arguments.length==4) tmpcell.style.width = width;
﻿  return tmpcell;
}


//AINSPECTOR.makeLinkIframe()
